import { createRouter, createWebHashHistory } from "vue-router"
import ajax from "../util/http"
import message from '../plugins/message'

const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        {
            path: '/',
            redirect: '/middle'
        }, {
            path: '/author',
            name: 'author',
            component: () => import('../views/Author.vue')
        }, {
            path: '/middle',
            name: 'middle',
            component: () => import('../views/Middle.vue')
        }, {
            path: '/index',
            name: 'home',
            component: () => import('../views/Home.vue')
        }, {
            path: '/signin',
            name: 'signin',
            meta: {
                title: '签到'
            },
            component: () => import('../views/star/Signin.vue')
        }, {
            path: '/signin/success',
            meta: {
                title: '签到成功'
            },
            component: () => import('../views/star/Success.vue')
        }, {
            path: '/interview',
            meta: {
                title: '小演员面试表'
            },
            component: () => import('../views/star/Form.vue')
        }, {
            path: '/interview/success',
            meta: {
                title: '保存成功'
            },
            component: () => import('../views/star/FormSuccess.vue')
        }, {
            path: '/interview/view',
            meta: {
                title: '报名表'
            },
            component: () => import('../views/star/Interview.vue')
        }, {
            path: '/second/register',
            name: 'register',
            meta: {
                title: '复试导演注册'
            },
            component: () => import('../views/second/Register.vue')
        }, {
            path: '/login',
            meta: {
                title: '工作人员登录'
            },
            component: () => import('../views/Login.vue')
        }, {
            path: '/number',
            name: 'number',
            meta: {
                title: '输入小演员编号',
                auth: true
            },
            component: () => import('../views/Number.vue')
        }, {
            path: '/first/comment',
            meta: {
                title: '小演员信息-初试导演',
                auth: true
            },
            component: () => import('../views/first/Comment.vue')
        }, {
            path: '/first/success',
            meta: {
                title: '保存成功',
                role: 'first'
            },
            component: () => import('../views/first/Success.vue')
        }, {
            path: '/second/comment',
            meta: {
                title: '小演员信息-复试导演',
                auth: true
            },
            component: () => import('../views/second/Comment.vue')
        }, {
            path: '/second/success',
            meta: {
                title: '保存成功',
                role: 'second'
            },
            component: () => import('../views/second/Success.vue')
        }, {
            path: '/host',
            name: 'host',
            meta: {
                title: '主持人',
                auth: true
            },
            component: () => import('../views/host/List.vue')
        }, {
            path: '/host/comment',
            meta: {
                title: '小演员信息-主持人',
                auth: true
            },
            component: () => import('../views/host/Comment.vue')
        }, {
            path: '/photographer',
            meta: {
                title: '上传照片',
                auth: true
            },
            component: () => import('../views/photographer/Upload.vue')
        }, {
            path: '/photographer/success',
            meta: {
                title: '保存成功',
                role: 'photographer'
            },
            component: () => import('../views/photographer/Success.vue')
        }
    ]
})

router.beforeEach((to, from, next) => {
    if (sessionStorage.getItem('scenes_bgimage')) {
        app.style.backgroundImage = `url(${sessionStorage.getItem('scenes_bgimage')})`;
    } else {
        if (to.name == 'home' || to.name == 'signin') {
            ajax
                .post("/star/scene/crew_info", {
                    cid: to.query.cid,
                })
                .then((res) => {
                    if (res.code == 1) {
                        document.title = res.data.crew_name;
                        sessionStorage.setItem("scenes_bgimage", res.data.scenes_bgimage);
                        app.style.backgroundImage = `url(${res.data.scenes_bgimage})`;
                    }
                });
        }
    }
    if (to.meta.auth) {
        let userinfo = JSON.parse(localStorage.getItem('userinfo'))
        let path = to.fullPath
        let role = '';
        let rolename = ''

        if (path.includes('first')) { role = 'first'; rolename = '初试导演'; }
        if (path.includes('second')) { role = 'second'; rolename = '复试导演'; }
        if (path.includes('host')) { role = 'host'; rolename = '主持人'; }
        if (path.includes('photographer')) { role = 'photographer'; rolename = '摄影师'; }
        //如果未登陆
        if (!userinfo || userinfo.role.name != rolename || userinfo.expiretime <= parseInt((new Date().getTime()) / 1000)) {
            next({
                path: '/login',
                query: {
                    role: role,
                    cid: to.query.cid
                }
            })
        }
    }

    if (['signin', 'author', 'register', 'home'].includes(to.name) && !to.query.cid) {
        message.$message({ content: '缺少剧组cid参数' })
    }
    else {
        next()
    }
    if (to.meta.title) {
        document.title = to.meta.title
    }
})

export default router
